#pragma once
class IDelegateHandler 
{
public:
	virtual ~IDelegateHandler() { }
	virtual DWORD Invoke(LPVOID) = 0;
};

template<typename T>
class DelegateHandler : public IDelegateHandler
{
public:
	DelegateHandler(T* pT, DWORD (T::*pFunc)(LPVOID))
		: m_pT(pT), m_pFunc(pFunc) { }

	DWORD Invoke(LPVOID value) 
	{
		return (m_pT->*m_pFunc)(value);
	}
private:
	T* m_pT;
	DWORD (T::*m_pFunc)(LPVOID);
};

class MsgHandler
{
public:
	void RegisterMsgFunc(DWORD dwMsgID, IDelegateHandler* h);
	void ExecuteMessage(LPVOID lp);
	void UnRegisterMsgFunc();
	~MsgHandler();
private:
	map<DWORD, IDelegateHandler*> m_mapHandler;
};

// register world message handle fuction
extern MsgHandler sMsgHandler;
#define REGISTER_WORLD_MSG(MSGID,OBJ,CLASS,FUNC) \
	{DelegateHandler<CLASS>* pHandler= new DelegateHandler<CLASS>(&OBJ, FUNC);\
	sMsgHandler.RegisterMsgFunc(MSGID, pHandler);}
